package com.qxsx.midi.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.qxsx.midi.R;
import com.qxsx.midi.utils.ByteReq;
import com.qxsx.midi.utils.FileCache;

import java.io.File;
import java.io.FileOutputStream;


public class MeKejiantActivity extends Activity implements View.OnClickListener {
    FrameLayout btnBack;
    PDFView pdfView;
    RequestQueue queue;
    String file_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kejian);
        Intent intent = getIntent();
        file_url = intent.getStringExtra("file_url");//得到ID
        // 返回按钮
        btnBack = (FrameLayout) findViewById(R.id.btn_msg_back);
        btnBack.setOnClickListener(this);

        queue = Volley.newRequestQueue(this);



        download(file_url);

    }

    @Override
    public void onClick(View v) {
        if (v == btnBack) {
            //TODO
            finish();
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }
    }



    public void download (final String url) {
        Log.e("download start", url);
        ByteReq request = new ByteReq(url, new Response.Listener<byte[]>() {
            @Override
            public void onResponse(byte[] response) {
                FileCache fc = new FileCache(getApplication());
                File f = fc.getFile(url);
                try {
                    FileOutputStream fos = new FileOutputStream(f);
                    fos.write(response);
                    fos.close();
                    showPdfByFile(f);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("tag", error.toString());
            }
        });
        queue.add(request);
    }

    public void showPdfByFile (File f) {
        pdfView = (PDFView)findViewById(R.id.pdfview);
        pdfView.fromFile(f)
                .defaultPage(1)
                .onPageChange(new OnPageChangeListener() {
                    @Override
                    public void onPageChanged(int i, int i1) {
                        Log.e("pdf", "dd");
                    }
                })
                .load();
    }

    public void showPdf (String pdfName) {
        pdfView = (PDFView)findViewById(R.id.pdfview);
        pdfView.fromAsset(pdfName)
                .defaultPage(1)
                .onPageChange(new OnPageChangeListener() {
                    @Override
                    public void onPageChanged(int i, int i1) {
                        Log.e("pdf", "dd");
                    }
                })
                .load();

//        pdfView.fromAsset(pdfName)
//                .pages(0, 2, 1, 3, 3, 3)
//                .defaultPage(1)
//                .showMinimap(false)
//                .enableSwipe(true)
//                .onDraw(new OnDrawListener() {
//                    @Override
//                    public void onLayerDrawn(Canvas canvas, float v, float v1, int i) {
//                    }
//                })
//                .onLoad(new OnLoadCompleteListener() {
//                    @Override
//                    public void loadComplete(int i) {
//                    }
//                })
//                .onPageChange(new OnPageChangeListener() {
//                    @Override
//                    public void onPageChanged(int i, int i1) {
//                    }
//                })
//                .load();
    }
}
